package modell;
import java.io.File;
import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;


public class Froghead extends BranchGroup{
	private ArrayList<Texture2D> textures_eye = null;
	private ArrayList<Texture2D> textures_head = null;
	private Texture2D texture_neck = null;
	private TextureAttributes text_attr = null;
	private JFrame textContent = null;
	
	public Froghead(JFrame textContent){
		textures_eye = new ArrayList<Texture2D>();
		textures_head = new ArrayList<Texture2D>();
		loadNeck("textures/gruen.png");
	    text_attr = new TextureAttributes();
	    text_attr.setTextureMode(TextureAttributes.MODULATE);
		this.textContent = textContent;
	    
	    loadNeck("textures/gruen.png");
	    loadEye("textures/eye1.jpg");
	    loadEye("textures/eye2.jpg");
	    loadEye("textures/eye3.jpg");
	    loadEye("textures/eye4.jpg");
	    loadHead("textures/head1.jpg");
	    loadHead("textures/head2.jpg");
	    loadHead("textures/head34.jpg");
		    
		Appearance ap_head = new Appearance();
		ap_head.setMaterial(new Material(new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(0.0f, 0.0f, 0.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 0.2f));
		
		Appearance ap_eye_l = new Appearance();
		ap_eye_l.setMaterial(new Material(new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(0.0f, 0.0f, 0.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 0.2f));
		
		Appearance ap_eye_r = new Appearance();
		ap_eye_r.setMaterial(new Material(new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(0.0f, 0.0f, 0.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 0.2f));
		
	    try {
			ap_head.setTexture(getHead());
			ap_head.setTextureAttributes(text_attr);
			ap_eye_l.setTexture(getEye());
			ap_eye_l.setTextureAttributes(text_attr);
			ap_eye_r.setTexture(getEye());
			ap_eye_r.setTextureAttributes(text_attr);
	    } catch (NullPointerException e) {
	    	System.err.println("Frog textures have not been loaded. "+e.toString());
	    }
		
		Appearance ap_hals = new Appearance();
		ap_hals.setMaterial(new Material(new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(0.0f, 1.0f, 0.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 
				new Color3f(1.0f, 1.0f, 1.0f), 1.0f));
		ap_hals.setTexture(texture_neck);
		ap_hals.setTextureAttributes(text_attr);
		
		// TransformGroup for scaling
		TransformGroup tGroup_scale = new TransformGroup();
		Transform3D scale_all = new Transform3D();
		scale_all.setScale(0.2);
		tGroup_scale.setTransform(scale_all);
		this.addChild(tGroup_scale);
		
		// Head.
		TransformGroup tGroup_head = new TransformGroup();
		Transform3D scale_head = new Transform3D(); 
		scale_head.setScale(new Vector3d(1.0, 0.5, 1.0 ));
		tGroup_head.setTransform(scale_head);
		tGroup_head.addChild(new Sphere(2.0f,Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS,ap_head));
		tGroup_scale.addChild(tGroup_head);
		
		// Eyes.
		float eye_size = (float)(Math.random() * 0.4 + 0.8);
		Transform3D translation_lefteye = new Transform3D();
		translation_lefteye.setTranslation(new Vector3f(-0.7f, 0.7f, 0.0f));
		TransformGroup tGroup_lefteye = new TransformGroup(translation_lefteye);
		Sphere lefteye= new Sphere(eye_size,
				Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS, ap_eye_l);
		tGroup_lefteye.addChild(lefteye);
		tGroup_scale.addChild(tGroup_lefteye);
			
		eye_size = (float)(Math.random() * 0.4 + 0.8);
		Transform3D translation_righteye = new Transform3D();
		translation_righteye.setTranslation(new Vector3f(0.7f, 0.7f, 0.0f));
		TransformGroup tGroup_righteye = new TransformGroup(translation_righteye);
		Sphere righteye= new Sphere(1.0f,
				Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS, ap_eye_r);
		
		tGroup_righteye.addChild(righteye);
		tGroup_scale.addChild(tGroup_righteye);
		
		// Hals		
		Transform3D translation = new Transform3D();
		translation.setTranslation(new Vector3f(0.0f, -0.7f, 0.0f));
		
		TransformGroup tGroup_cylinder = new TransformGroup(translation);
		Cylinder cylinder = new Cylinder(0.3f, 1.2f, ap_hals);
		tGroup_cylinder.addChild(cylinder);
		tGroup_scale.addChild(tGroup_cylinder);
	}
	
	String getAbsolutePath(String filename) {
		File file = new File(filename);
		file = file.getAbsoluteFile();
		return file.getAbsolutePath();
	}
	
	void loadNeck(String filename){
		filename = getAbsolutePath(filename);
		ImageComponent2D image = null;
		try {
			TextureLoader loader = new TextureLoader(filename, textContent);
			image = loader.getImage();
		} catch (Throwable t) {
			System.out.println("file not found: "+filename);
		}
		
		if(image == null) {
	         System.out.println("load failed for texture: "+filename);
		}
       
		texture_neck = new Texture2D(Texture.BASE_LEVEL, 
	 		  Texture.RGBA,
	           image.getWidth(), 
	           image.getHeight());
		texture_neck.setImage(0, image);
		texture_neck.setEnable(true);
	}
	
	void loadHead(String filename){	
		filename = getAbsolutePath(filename);
		ImageComponent2D image = null;
		try {
			TextureLoader loader = new TextureLoader(filename, textContent);
			image = loader.getImage();
		} catch (Throwable t) {
			System.out.println("file not found: "+filename);
		}

	    if(image == null) {
	        System.out.println("load failed for texture: "+filename);
	        return;
	    }
	     
	    Texture2D texture = new Texture2D(Texture.BASE_LEVEL, 
	        Texture.RGBA,
	        image.getWidth(), 
	        image.getHeight());
	    texture.setImage(0, image);
	    texture.setEnable(true);
	    
	    textures_head.add(texture);
    }
	
	void loadEye(String filename){	
		filename = getAbsolutePath(filename);
		ImageComponent2D image = null;
		try {
			TextureLoader loader = new TextureLoader(filename, textContent);
			image = loader.getImage();
		} catch (Throwable t) {
			System.out.println("file not found: "+filename);
		}

	    if(image == null) {
	        System.out.println("load failed for texture: "+filename);
	        return;
	    }
	     
	    Texture2D texture = new Texture2D(Texture.BASE_LEVEL, 
	        Texture.RGBA,
	        image.getWidth(), 
	        image.getHeight());
	    texture.setImage(0, image);
	    texture.setEnable(true);
	    
	    textures_eye.add(texture);
    }
	
	Texture2D getEye() {
		if(textures_eye.size() == 0)
		{
			return null;
		}
		int pos = (int)(Math.random() * (double)textures_eye.size());
		if(pos == textures_eye.size())
			pos--;
		return textures_eye.get(pos);
	}
	
	Texture2D getHead() {
		if(textures_head.size() == 0)
		{
			return null;
		}
		int pos = (int)(Math.random() * (double)textures_head.size());
		if(pos == textures_head.size())
			pos--;
		return textures_head.get(pos);
	}
	
}
 